<?php
include( "agent/onlinebet_header5.php" );
$bettype = 36;
$sql = "select * from bet where bettype={$bettype} and lotto_id=" . $lotto_id . " and rame in (0,9)";
if ($curruser['acl'] != 255)
{
    $userlist = GetUserList($curruser['id']);
    if ($userlist == "")
    {
        $ui->showErrorPage("<p>还没有建立会员！</p>");
        exit();
    }
    $sql .= " and user_id in (" . $userlist . ")";
}
$g_disk_code = array(1 => "a", 2 => "b", 3 => "c", 4 => "d");
$disk_code = "a";
if (0 < $disk)
{
    $sql .= " and disk=" . $disk;
    $disk_code = $g_disk_code[$disk];
}
$percent = 0;
$betcount = array();
$lostwin = array();
$totalamount = array();
$betids = array(21, 31, 41, 51);
$ary = array();
foreach ($betids as $betid)
{
    $totalamount[$betid] = 0;
    $betcount[$betid] = 0;
    $ary[$betid] = array();
    $i = 0;
    while ($i < 1000)
    {
        $ary[$betid][$i] = array("num" => "", "amount" => 0);
        $lostwin[$betid][$i] = 0;
        ++$i;
    }
}
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$sql .= " order by amount desc, bettype, numbers";
$rs = getdb($sql);
while (!$rs->eof)
{
    if ($split == 1)
    {
        if ($theacl == 2)
        {
            $percent = $rs->f['mpercent'];
        }
        else if ($theacl == 3)
        {
            $percent = $rs->f['percent2'];
        }
        else if ($theacl == 4)
        {
            $percent = $rs->f['percent3'];
        }
        else if ($theacl == 5)
        {
            $percent = $rs->f['percent4'];
        }
        else
        {
            $percent = $rs->f['upercent'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
    }
    else
    {
        $percent = 1;
    }
    $p = explode("|", $rs->f['numbers']);
    $type = $p[0];
    $num = $p[1];
    $tmp = $percent * $rs->f['amount'];
    if ($betmin <= $tmp)
    {
        $n = Scan_LM_Array($type, $num, $ary, 1000);
        if ($n < 0)
        {
            $j = Empty_LM_Array($type, $ary, 1000);
            if (0 <= $j)
            {
                $ary[$type][$j]['num'] = $num;
                $ary[$type][$j]['amount'] = $tmp;
                ++$betcount[$type];
            }
        }
        else
        {
            $ary[$type][$n]['amount'] += $tmp;
        }
    }
    $totalamount[$type] += $tmp;
    $rs->movenext();
}
$rs->close();
if (2 < $theacl)
{
    if ($theacl == 255)
    {
        $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and bettype={$bettype} and rame in (0,9)";
    }
    else
    {
        $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and uperid" . $theacl . "=" . $userid . " and bettype={$bettype} and rame in (0,9)";
    }
    $rs = getdb($sql);
    while (!$rs->eof)
    {
        $percent = 1;
        $backs = 100;
        if ($theacl == 255)
        {
            $backs = $rs->f['back5'];
            $percent = $rs->f['percent255'] - 0;
        }
        else if ($theacl == 5)
        {
            $backs = $rs->f['back4'];
            $percent = $rs->f['percent5'];
        }
        else if ($theacl == 4)
        {
            $backs = $rs->f['back3'];
            $percent = $rs->f['percent4'];
        }
        else if ($theacl == 3)
        {
            $backs = $rs->f['back2'];
            $percent = $rs->f['percent3'];
        }
        if (0 < $percent)
        {
            $percent /= 100;
        }
        if ($split != 1)
        {
            $percent = 1;
        }
        $p = explode("|", $rs->f['numbers']);
        $type = $p[0];
        $num = $p[1];
        $tmp = $percent * $rs->f['amount'];
        if ($betmin <= $tmp)
        {
            $n = Scan_LM_Array($type, $num, $ary, 1000);
            if ($n < 0)
            {
                $j = Empty_LM_Array($type, $ary, 1000);
                if (0 <= $j)
                {
                    $ary[$type][$j]['num'] = $num;
                    $ary[$type][$j]['amount'] = $tmp;
                    ++$betcount[$type];
                }
            }
            else
            {
                $ary[$type][$n]['amount'] += $tmp;
            }
        }
        $totalamount[$type] += $tmp;
        $rs->movenext();
    }
}

$rs->close();
if ($theacl == 255)
{
    $sql = "select * from bet8 where lotto_id=" . $lotto_id . " and bettype={$bettype} and rame in (0,9)";
}
else
{
    $sql = "select * from bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and bettype={$bettype} and rame in (0,9)";
}
$rs = getdb($sql);
while (!$rs->eof)
{
    $p = explode("|", $rs->f['numbers']);
    $type = $p[0];
    $num = $p[1];
    $tmp = $rs->f['amount'];
    $n = Scan_LM_Array($type, $num, $ary, 1000);
	
    if ($n < 0)
    {
        $j = Empty_LM_Array($type, $ary, 1000);
		
        if (0 <= $j)
        {
            $ary[$type][$j]['num'] = $num;
            $ary[$type][$j]['amount'] = 0 - $tmp;
            ++$betcount[$type];
        }
    }
    else
    {
       
		$ary[$type][$n]['amount'] -= $rs->f['amount'];
		 
    }
	
    $totalamount[$type] -= $tmp;
	
    $rs->movenext();
}

$rs->close();
$sql2 = " bet2 where lotto_id=" . $lotto_id . " and user_id=" . $userid . " and ";
$list = array();
foreach ($betids as $betid)
{
    $list[$betid] = "";
    if (0 < $betcount[$betid])
    {
        $i = 0;
        while ($i < 1000)
        {
            $lostwin[$betid][$i] = 0 - $ary[$betid][$i]['amount'];
            ++$i;
        }
        asort($lostwin[$betid]);
        reset($lostwin[$betid]);
        $i = 0;
        foreach ($lostwin[$betid] as $key => $val)
        {
            if ($i < 30)
            {
                $c = $ary[$betid][$key]['num'];
                $n = $ary[$betid][$key]['amount'];
                if ($c != "")
                {
                    $numbers = "{$betid}|{$c}";
                    $url = "op.php?op=agent&fp=betlist1&dt1=" . $open . "&dt2=" . $open . "&bettype={$bettype}&page=-1&order=2&numbers=" . $numbers;
                    $list .= $betid;
                    $list .= $betid;
                    $list .= $betid;
                    ++$i;
                }
            }
        }
    }
    $tpl->set("money" . $betid, $totalamount[$betid]);
    $tpl->set("list" . $betid, $list[$betid]);
}
$tpl->set("dt", $open);
$idisk = $disk == 0 ? 1 : $disk;
$rs = getdb("select * from rate36 where lotto_id={$lotto_id} and disk={$idisk} order by subid");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
while (!$rs->eof)
{
    $subid = $rs->f['subid'];
    $html = "<table width='130' border='1'><tr>";
    $i = 0;
    while ($i <= 9)
    {
        $html .= "<td>" . $i . ")&nbsp;<font color='#FF0000'>" . $rs->f["r" . $i] . "</font></td>";
        if ($i % 3 == 2)
        {
            $html .= "</tr><tr>";
        }
        ++$i;
    }
    $html .= "</tr></table>";
    $tpl->set("r36_{$subid}", $html);
    $rs->next();
}
$tpl->load("main", "onlinebet7.htm");
$tpl->show("main");
$ui->footer();